package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.OsOrderDto;
import com.yc.sole.erp.entity.OsOrder;
import com.yc.sole.erp.mapper.OsOrderMapper;
import com.yc.sole.erp.request.OsOrderQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-13
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class OsOrderRepository extends ServiceImpl<OsOrderMapper, OsOrder> {

    /**
     * 新增
     *
     * @param osOrderDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOsOrder(OsOrderDto osOrderDto) {
        OsOrder entity = new OsOrder();
        BeanUtil.copyProperties(osOrderDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param osOrderDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateOsOrderById(OsOrderDto osOrderDto) {
        OsOrder entity = new OsOrder();
        BeanUtil.copyProperties(osOrderDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteOsOrderByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<OsOrderDto> selectOsOrderByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), OsOrderDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public OsOrderDto selectOsOrderById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), OsOrderDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<OsOrderDto> selectOsOrderPage(Paging page, OsOrderQueryReq queryReq) {
        LambdaQueryWrapper<OsOrder> wrapper = Wrappers.<OsOrder>lambdaQuery()
                .eq(Objects.nonNull(queryReq.getCustomerId()), OsOrder::getCustomerId, queryReq.getCustomerId())
                .eq(Objects.nonNull(queryReq.getProductId()), OsOrder::getProductId, queryReq.getProductId())
                .eq(Objects.nonNull(queryReq.getSupplierId()), OsOrder::getSupplierId, queryReq.getSupplierId())
                //.eq(Objects.nonNull(queryReq.getOsOrderState()), OsOrder::getOsOrderState, queryReq.getOsOrderState())
                .like(CharSequenceUtil.isNotBlank(queryReq.getOsOrderNo()), OsOrder::getOsOrderNo, queryReq.getOsOrderNo())
                .like(CharSequenceUtil.isNotBlank(queryReq.getOrderNo()), OsOrder::getOrderNo, queryReq.getOrderNo())
                //.orderByAsc(OsOrder::getOsOrderState)
                .orderByDesc(OsOrder::getId);
        if (CollUtil.isNotEmpty(queryReq.getDateRange())
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(0))
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(1))) {
            wrapper.between(OsOrder::getDate, queryReq.getDateRange().get(0), queryReq.getDateRange().get(1));
        }
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), wrapper), OsOrderDto.class);
    }

    /**
     * 查询订单状态的数量
     *
     * @return
     */
    /*public List<StateCountResp> selectOsOrderStateCount(OsOrderQueryReq queryReq) {
        return this.baseMapper.selectOsOrderStateCount(queryReq);
    }*/

    /**
     * 根据订单id查询是否存在外发订单
     *
     * @param orderId
     * @return
     */
    public Boolean existByOrderId(Long orderId) {
        return this.exists(Wrappers.<OsOrder>lambdaQuery().eq(OsOrder::getOrderId, orderId));
    }

    /**
     * 根据订单id查询是否存在外发订单
     *
     * @param orderId
     * @return
     */
    public OsOrderDto selectByOrderId(Long orderId) {
        LambdaQueryWrapper<OsOrder> wrapper = Wrappers.<OsOrder>lambdaQuery().eq(OsOrder::getOrderId, orderId);
        return BeanUtil.copyProperties(this.getOne(wrapper), OsOrderDto.class);
    }
}
